/*
 * Copyright (c) 2019 Author: Fancyears Milos Malvis @1243093366@qq.com
 * ALL COPYRIGHT REVERSED.
 */

package com.fancyears.controller;

import com.alibaba.fastjson.JSONObject;
import com.fancyears.dao.i.user.UserDao;
import com.fancyears.dto.UserLoginDTO;
import com.fancyears.service.impl.encryption.RSAServiceImpl;
import com.fancyears.global.GlobalSetting;
import com.fancyears.model.user.User;
import com.fancyears.model.user.UserDetail;
import com.fancyears.service.i.user.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * @Author: Fancyears Milos Malvis
 * @Date: Created in  2019/5/8 14:01
 * @Modified By:
 */
@Controller
@RequestMapping("/user")
@Slf4j//这个注解是lombok的
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    RSAServiceImpl rsaUtil;


    @Autowired
    UserDao userDao;
    @RequestMapping(value = "/getUser")
    @ResponseBody
    public String getUser(@RequestParam("id") Long id, Model model) {
        User user = userService.getUser(id);
        log.warn(user.toString());
        return user.getId().toString();
    }

    @RequestMapping(value = "debugPage")
    public String signUp(@RequestParam String path) {
        return path;//视图解析器自动加上web/WEB-INF/view/
//        return "success";
    }


    @RequestMapping(value = "register")
    @ResponseBody
    public boolean register(@RequestBody User user, HttpServletRequest request, HttpServletResponse response) {
////        User user=new User();
////        user.setUsername(username);
////        user.setPassword(password);
//        if (EncryptCodeUtils.isNotEmpty(user.getUsername()) && EncryptCodeUtils.isNotEmpty(user.getPassword())) {
//            Cookie sid = new Cookie("sid", request.getSession().getId());
//            sid.setPath("/");
//            sid.setMaxAge(GlobalSetting.ONE_YEAR);
//            response.addCookie(sid);
//            return true;
//        }
//        return false;
////        return userService.register(user);
        return false;
    }


    @RequestMapping(value = "login")
    @ResponseBody
    public JSONObject login(@RequestBody UserLoginDTO user, HttpServletRequest request, HttpServletResponse response) throws IOException {
        return userService.doLogin2(user, request, response);
    }

    @RequestMapping(value = "insertForRollback")
    @ResponseBody
    public JSONObject insertForRollback(@RequestParam int id,@RequestParam int type, HttpServletRequest request, HttpServletResponse response) throws IOException {
//        return userService.insert(id,type);
        User user = new User();
        user.setUsername("xxx");
        user.setPassword("xxx");
        user.setId((long) id);
        userDao.insert(user);
        userDao.insert(user);
        if(type==2){
            throw new RuntimeException("手动回滚");
        }
        return null;
    }

    @RequestMapping(value = "trigger")
    @ResponseBody
    public boolean trigger(HttpSession session) {
        JSONObject result = new JSONObject();
        result.put(GlobalSetting.USERNAME, session.getAttribute(GlobalSetting.USERNAME));
        result.put(GlobalSetting.STATUS, GlobalSetting.STATUS_ENUM.OK);
        return true;
    }

    @RequestMapping(value = "signout")
    @ResponseBody
    public JSONObject signout(HttpSession session) {
        return userService.doSignout(session);
    }

    @RequestMapping(value = "testForCoockie")
    public String testForCoockie(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            return "client/login";
        } else {
            String sid = null;
            for (Cookie cookie : cookies) {
                if ((sid = cookie.getName()).equals("sid")) {
                    System.out.println(sid);
                    return "success";
                }
            }
            return GlobalSetting.ERROR_PAGE;
        }
    }


    @RequestMapping(value = "registerWithDetail")
    public String registerWithDetail(@RequestParam String username, @RequestParam String password, Model model) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        UserDetail userDetail = new UserDetail();
        userDetail.setSex((byte) 1);
        boolean success = userService.insertWithDetail(user, userDetail);
        model.addAttribute(GlobalSetting.MESSAGE, success);
        return "success";
    }

    /**
     * 获取session公钥
     *
     * @return
     */
    @GetMapping("getSession")
    public Map<String, String> getSession(HttpSession session) throws Exception {
        return rsaUtil.getSession(session);
    }
}
